// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.radio.script;

import java.util.ArrayList;
import java.util.List;
import zombie.radio.globals.CompareMethod;
import zombie.radio.globals.CompareResult;
import zombie.radio.globals.RadioGlobal;

public final class ConditionContainer implements ConditionIter {
    private List<ConditionIter> conditions = new ArrayList();
    private OperatorType operatorType = OperatorType.NONE;

    public ConditionContainer() {
        this(OperatorType.NONE);
    }

    public ConditionContainer(OperatorType operator) {
        this.operatorType = operator;
    }

    @Override
    public CompareResult Evaluate() {
        boolean _boolean = false;

        for (int _int = 0; _int < this.conditions.size(); _int++) {
            ConditionIter conditionIter = (ConditionIter)this.conditions.get(_int);
            CompareResult compareResult = conditionIter != null ? conditionIter.Evaluate() : CompareResult.Invalid;
            if (compareResult.equals(CompareResult.Invalid)) {
                return compareResult;
            }

            OperatorType operatorTypex = conditionIter.getNextOperator();
            if (_int == this.conditions.size() - 1) {
                return !operatorTypex.equals(OperatorType.NONE) ? CompareResult.Invalid : (!_boolean ? compareResult : CompareResult.False);
            }

            if (operatorTypex.equals(OperatorType.OR)) {
                if (!_boolean && compareResult.equals(CompareResult.True)) {
                    return compareResult;
                }

                _boolean = false;
            } else if (operatorTypex.equals(OperatorType.AND)) {
                _boolean = _boolean || compareResult.equals(CompareResult.False);
            } else if (operatorTypex.equals(OperatorType.NONE)) {
                return CompareResult.Invalid;
            }
        }

        return CompareResult.Invalid;
    }

    @Override
    public OperatorType getNextOperator() {
        return this.operatorType;
    }

    @Override
    public void setNextOperator(OperatorType _operatorType) {
        this.operatorType = _operatorType;
    }

    public void Add(ConditionContainer container) {
        this.conditions.add(container);
    }

    public void Add(RadioGlobal A, RadioGlobal B, CompareMethod compareMethod, OperatorType nextOperator) {
        ConditionContainer.Condition condition = new ConditionContainer.Condition(A, B, compareMethod, nextOperator);
        this.conditions.add(condition);
    }

    private static final class Condition implements ConditionIter {
        private OperatorType operatorType = OperatorType.NONE;
        private CompareMethod compareMethod;
        private RadioGlobal valueA;
        private RadioGlobal valueB;

        public Condition(RadioGlobal radioGlobal0, RadioGlobal radioGlobal1, CompareMethod compareMethodx) {
            this(radioGlobal0, radioGlobal1, compareMethodx, OperatorType.NONE);
        }

        public Condition(RadioGlobal radioGlobal0, RadioGlobal radioGlobal1, CompareMethod compareMethodx, OperatorType operatorTypex) {
            this.valueA = radioGlobal0;
            this.valueB = radioGlobal1;
            this.operatorType = operatorTypex;
            this.compareMethod = compareMethodx;
        }

        @Override
        public CompareResult Evaluate() {
            return this.valueA != null && this.valueB != null ? this.valueA.compare(this.valueB, this.compareMethod) : CompareResult.Invalid;
        }

        @Override
        public OperatorType getNextOperator() {
            return this.operatorType;
        }

        @Override
        public void setNextOperator(OperatorType operatorTypex) {
            this.operatorType = operatorTypex;
        }
    }
}
